﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents an HTML form for storing and submitting data.
    /// </summary>
    [Serializable]
    public class Form : Element {
        private IList<string> _accept;
        private IList<string> _acceptcharset;
        private string _action;
        private bool? _autocomplete;
        private string _enctype;
        private FormMethodChoices? _method;
        private string _name;
        private bool _novalidate;
        private string _target;

        /// <summary>
        /// Constructs a new instance of a Form tag.
        /// </summary>
        public Form() {
            _accept = new List<string>();
            _acceptcharset = new List<string>();
        }

        /// <summary>
        /// Gets a list of all content types that the server accepts.
        /// </summary>
        [Obsolete("Deprecated in HTML5 - Use the \"accept\" attribute of the individual input tag.")]
        public IList<string> Accept {
            get { return _accept; }
        }

        /// <summary>
        /// Gets a list of character encodings that the server accepts.
        /// </summary>
        public IList<string> AcceptCharset {
            get { return _acceptcharset; }
        }

        /// <summary>
        /// Gets the URL that the form data is submitted to.
        /// </summary>
        public string Action {
            get { return _action; }
            set { _action = value; }
        }

        /// <summary>
        /// Determines whether the controls can have their default values
        /// automatically filled in by the client.
        /// </summary>
        public bool? AutoComplete {
            get { return _autocomplete; }
            set { _autocomplete = value; }
        }

        /// <summary>
        /// Determines the MIME type to submit the form with.
        /// </summary>
        public string EncType {
            get { return _enctype; }
            set { _enctype = value; }
        }

        /// <summary>
        /// Determines the HTTP method that the form is submitted by.
        /// </summary>
        public FormMethodChoices? Method {
            get { return _method; }
            set { _method = value; }
        }

        /// <summary>
        /// Determines the name of the form.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use the \"id\" attribute instead.")]
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// If true, the form will not be validated for errors before
        /// submission.
        /// </summary>
        public bool NoValidate {
            get { return _novalidate; }
            set { _novalidate = value; }
        }

        /// <summary>
        /// Determines the target resource in which to display the response.
        /// </summary>
        public string Target {
            get { return _target; }
            set { _target = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _accept, "accept", ',');
            AttributeWriter.Write(sb, _acceptcharset, "accept-charset", ' ');
            AttributeWriter.Write(sb, _action, "action");
            if (_autocomplete != null) {
                AttributeWriter.Write(sb, (_autocomplete.Value ? "on" : "off"),
                    "autocomplete");
            }
            AttributeWriter.Write(sb, _enctype, "enctype");
            AttributeWriter.Write(sb, _method, "method", true);
            AttributeWriter.Write(sb, _name, "name");
            AttributeWriter.Write(sb, _novalidate, "novalidate");
            AttributeWriter.Write(sb, _target, "target");
        }
    }
}
